package com.example.system.controller.exam;

import com.example.common.core.domain.R;
import com.example.common.core.domain.ResultCode;
import com.example.common.core.domain.TableDataInfo;
import com.example.system.Service.exam.impl.ExamService;
import com.example.system.domain.exam.dto.ExamAddDTO;
import com.example.system.domain.exam.dto.ExamQuestionDeleteDTO;
import com.example.system.domain.exam.dto.ExamQueryDTO;
import com.example.system.domain.exam.dto.ExamQuestionAddDTO;
import com.example.system.domain.exam.vo.ExamVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/exam")
@RestController
@Slf4j
public class ExamController {

    @Autowired
    private ExamService examService;

    @GetMapping("/list")
    public TableDataInfo list(ExamQueryDTO examQueryDTO) {
        log.info("/exam/list -> ExamQueryDTO:{}", examQueryDTO.toString());
        return examService.list(examQueryDTO);
    }

    @PostMapping("/add")
    public R<Long> add(@RequestBody ExamAddDTO examAddDTO) {
        log.info("/exam/add -> ExamQueryDTO:{}", examAddDTO.toString());
        return examService.add(examAddDTO);
    }

    @PostMapping("/questionAdd")
    public void questionAdd(@RequestBody ExamQuestionAddDTO examQuestionAddDTO) {
        log.info("/exam/questionAdd -> examQuestionAddDTO:{}",
                examQuestionAddDTO.toString());
        examService.questionAdd(examQuestionAddDTO);
    }

    @GetMapping("/getExamQuestionList")
    public TableDataInfo getExamQuestionList(Long examId) {
        log.info("/exam/getExamQuestionList -> examId:{}", examId);
        return examService.getExamQuestionList(examId);
    }

    @GetMapping("/getExamDetailsById")
    public R<ExamVo> getExamDetailsById(Long examId) {
        log.info("/exam/getExamDetailsById -> examId:{}", examId);
        return R.ok(examService.getExamById(examId));
    }

    @PostMapping("/questionDelete")
    public R<Void> questionDelete(@RequestBody ExamQuestionDeleteDTO questionDeleteDTO) {
        log.info("/exam/questionDelete -> examQuestionAddDTO:{}",
                questionDeleteDTO.toString());
        int delete = examService.questionDelete(questionDeleteDTO);
        if (delete != 1) {
            return R.fail();
        } else {
            return R.ok();
        }
    }

    @PostMapping("/publish")
    public R<Void> publish(Long examId) throws JsonProcessingException {
        log.info("/exam/publish -> examId:{}", examId);
        int publish = examService.publish(examId);
        if (publish != 1) {
            return R.fail();
        }
        return R.ok();
    }

    @PostMapping("/unpublish")
    public R<Void> unpublish(Long examId) throws JsonProcessingException {
        log.info("/exam/unpublish -> examId:{}", examId);
        int publish = examService.unpublish(examId);
        if (publish != 1) {
            return R.fail();
        }
        return R.ok();
    }
}
